﻿/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Interfaces.Imaging;
using System.Collections.ObjectModel;
using MyPhotoIndex.ImageBank;
using MyPhotoIndex.Utilities;
using System.Windows.Forms;

namespace MyPhotoIndex.MainMDI
{
    public delegate void ProcessOneImageDelegate<T>(IImageData imageData, T state);
    public delegate bool ProcessOneImageDelegateError<T>(IImageData imageData, T state, Exception ex);

    public class ImageBatchOperationProcessor
    {
        public static void ProcessBatch<T>(ReadOnlyCollection<int> imageList, string description, ProcessOneImageDelegate<T> processOneImageDelegate, T state)
        {
            ProcessBatch<T>(imageList, description, processOneImageDelegate, state, null);
        }

        public static void ProcessBatch<T>(ReadOnlyCollection<int> imageList, string description, ProcessOneImageDelegate<T> processOneImageDelegate, T state, ProcessOneImageDelegateError<T> errorDelegate)
        {
            //copy the list to aviod collection change exceptions.
            List<int> selectedImages = new List<int>(imageList);
            if (selectedImages.Count == 1)
            {
                IImageData oneImageData = Storage.Instance[selectedImages[0]];
                processOneImageDelegate(oneImageData, state);
                return;
            }

            ApplicationContext.Instance.ProcessProgress.Maximum = selectedImages.Count * 2;
            ApplicationContext.Instance.ProcessProgress.ProcessDescription = description;
            ApplicationContext.Instance.ProcessProgress.Running = true;

            foreach (int selectedImageId in selectedImages)
            {
                ApplicationContext.Instance.ProcessProgress.Value++;

                IImageData imageData = Storage.Instance[selectedImageId];

                try
                {
                    processOneImageDelegate(imageData, state);
                }
                catch (Exception ex)
                {
                    if (errorDelegate != null)
                    {
                        bool result = errorDelegate.Invoke(imageData, state, ex);
                    }

                    Logger.Log(ex);
                }

                ApplicationContext.Instance.ProcessProgress.Value++;
                Application.DoEvents();
            }

            ApplicationContext.Instance.ProcessProgress.Running = false;
        }
    }
}
